package algorithm;

import java.util.Random;

public class Rule3Square extends Rule1Square {
	
	private int d; // rule parameter
	private byte[] s; // switching strategy

	/**
	 * 
	 * @param n - size of the matrix
	 * @param S - parameter of the simulation
	 * @param R - parameter of the simulation
	 * @param T - parameter of the simulation
	 * @param P - parameter of the simulation
	 * @param periodic
	 * @param d - number of defectors
	 */
	public Rule3Square(int n, int S, int R, int T,int P, boolean periodic, int d) {
		// TODO Auto-generated constructor stub
		super(n, S, R, T, P, periodic);
		this.d = d;
		s = new byte[nvert];
	}

	@Override
	public void cycle(byte[] L) {
		// TODO Auto-generated method stub
		super.cycle(L);
		Random gen = new Random();
		for (int i = 0; i < nvert; i++) {
			if (s[i] == 0) {
				byte strategy = (byte) gen.nextInt(2); 
				if (L[i] != strategy) {
					L[i] = strategy;
					if (L[i] == 0) {
						C--;
					} else {
						C++;
					}
				} 
			}
		}
	}

	/**
	 * Initialize the matrix with the given number of agents who are choosing their strategy randomly
	 */
	@Override
	public byte[] init() {
		// TODO Auto-generated method stub
		Random gen = new Random();
		
		for (int i = 0; i < nvert; i++) {
			s[i] = -1;
		}
		
		for (int i = 0; i < d; i++) {
			int rand = gen.nextInt(nvert);
			while (s[rand] != -1) {
				rand = gen.nextInt(nvert);
			}
			s[rand] = 0;
		}
		
		return super.init();
	}
}
